package com.saint.easemobui;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.hyphenate.chat.EMMessage;
import com.saint.easemobui.activity.ChatActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/11/18.
 */

public class EMMessageReceiver extends BroadcastReceiver {
    public static final String MSG_FLAG = "EMMESSAGE_FLAG";

    @Override
    public void onReceive(Context context, Intent intent) {
//        try {

        if (intent.getAction().equals(MSG_FLAG)) {
            // 跳转到聊天界面，开始聊天
//            Intent intent1 = new Intent(context, ChatActivity.class);
//            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            // EaseUI封装的聊天界面需要这两个参数，聊天者的username，以及聊天类型，单聊还是群聊
//            intent1.putExtra("userId", intent.getStringExtra("userId"));
//            intent1.putExtra("chatType", EMMessage.ChatType.Chat);
//            context.startActivity(intent);


            noti(context, intent);
//                }
        }

//        } catch (Throwable e) {
//            try {
//                e.printStackTrace();
////                Intent resultIntent = new Intent(context, BeforeWelcomeActivity.class);
////                resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
////                context.startActivity(resultIntent);
//
//
//                Log.e("EASEUI" , " 通知跳转报错" );
//            } catch (Throwable e1) {
//                e1.printStackTrace();
//            }
//        }
    }


    private void noti(Context context, Intent dataIntent) {
        Log.d("MyReceiver", "noti");
        NotificationManager manager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(context, ChatActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("userId", dataIntent.getStringExtra("userId"));
        intent.putExtra("chatType", EMMessage.ChatType.Chat);

        PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle(context.getResources().getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("新的聊天消息")
                .setTicker(context.getResources().getString(R.string.app_name))
                .setContentIntent(pi)
                .setDefaults(Notification.DEFAULT_ALL)
//                .setOngoing(true)
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);
    }
}
